home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / berkeleydb1.73 / Berkeley_db / btree / bt_put.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  8.4 KB  |  323 lines  |  [TEXT/MPS ]

  1. /*-
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Mike Olson.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)bt_put.c    8.3 (Berkeley) 9/16/93";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #if defined(macintosh) && (defined(powerc) || defined(__powerc))
  42. #include "OurMalloc.h"
  43. #endif
  44.  
  45. #include <sys/types.h>
  46.  
  47. #include <errno.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51.  
  52. #include <db.h>
  53. #include "btree.h"
  54.  
  55. static EPG *bt_fast __P((BTREE *, const DBT *, const DBT *, int *));
  56.  
  57. /*
  58.  * __BT_PUT -- Add a btree item to the tree.
  59.  *
  60.  * Parameters:
  61.  *    dbp:    pointer to access method
  62.  *    key:    key
  63.  *    data:    data
  64.  *    flag:    R_NOOVERWRITE
  65.  *
  66.  * Returns:
  67.  *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
  68.  *    tree and R_NOOVERWRITE specified.
  69.  */
  70. int
  71. __bt_put(dbp, key, data, flags)
  72.     const DB *dbp;
  73.     DBT *key;
  74.     const DBT *data;
  75.     u_int flags;
  76. {
  77.     BTREE *t;
  78.     DBT tkey, tdata;
  79.     EPG *e;
  80.     PAGE *h;
  81.     indx_t index, nxtindex;
  82.     pgno_t pg;
  83.     size_t nbytes;
  84.     int dflags, exact, status;
  85.     char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
  86.  
  87.     t = dbp->internal;
  88.  
  89.     /* Toss any page pinned across calls. */
  90.     if (t->bt_pinned != NULL) {
  91.         mpool_put(t->bt_mp, t->bt_pinned, 0);
  92.         t->bt_pinned = NULL;
  93.     }
  94.  
  95.     switch (flags) {
  96.     case R_CURSOR:
  97.         if (!ISSET(t, B_SEQINIT))
  98.             goto einval;
  99.         if (ISSET(t, B_DELCRSR))
  100.             goto einval;
  101.         break;
  102.     case 0:
  103.     case R_NOOVERWRITE:
  104.         break;
  105.     default:
  106. einval:        errno = EINVAL;
  107.         return (RET_ERROR);
  108.     }
  109.  
  110.     if (ISSET(t, B_RDONLY)) {
  111.         errno = EPERM;
  112.         return (RET_ERROR);
  113.     }
  114.  
  115.     /*
  116.      * If the key/data won't fit on a page, store it on indirect pages.
  117.      * Only store the key on the overflow page if it's too big after the
  118.      * data is on an overflow page.
  119.      *
  120.      * XXX
  121.      * If the insert fails later on, these pages aren't recovered.
  122.      */
  123.     dflags = 0;
  124.     if (key->size + data->size > t->bt_ovflsize) {
  125.         if (key->size > t->bt_ovflsize) {
  126. storekey:        if (__ovfl_put(t, key, &pg) == RET_ERROR)
  127.                 return (RET_ERROR);
  128.             tkey.data = kb;
  129.             tkey.size = NOVFLSIZE;
  130.             memmove(kb, &pg, sizeof(pgno_t));
  131.             memmove(kb + sizeof(pgno_t),
  132.                 &key->size, sizeof(size_t));
  133.             dflags |= P_BIGKEY;
  134.             key = &tkey;
  135.         }
  136.         if (key->size + data->size > t->bt_ovflsize) {
  137.             if (__ovfl_put(t, data, &pg) == RET_ERROR)
  138.                 return (RET_ERROR);
  139.             tdata.data = db;
  140.             tdata.size = NOVFLSIZE;
  141.             memmove(db, &pg, sizeof(pgno_t));
  142.             memmove(db + sizeof(pgno_t),
  143.                 &data->size, sizeof(size_t));
  144.             dflags |= P_BIGDATA;
  145.             data = &tdata;
  146.         }
  147.         if (key->size + data->size > t->bt_ovflsize)
  148.             goto storekey;
  149.     }
  150.  
  151.     /* Replace the cursor. */
  152.     if (flags == R_CURSOR) {
  153.         if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
  154.             return (RET_ERROR);
  155.         index = t->bt_bcursor.index;
  156.         goto delete;
  157.     }
  158.  
  159.     /*
  160.      * Find the key to delete, or, the location at which to insert.  Bt_fast
  161.      * and __bt_search pin the returned page.
  162.      */
  163.     if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
  164.         if ((e = __bt_search(t, key, &exact)) == NULL)
  165.             return (RET_ERROR);
  166.     h = e->page;
  167.     index = e->index;
  168.  
  169.     /*
  170.      * Add the specified key/data pair to the tree.  If an identical key
  171.      * is already in the tree, and R_NOOVERWRITE is set, an error is
  172.      * returned.  If R_NOOVERWRITE is not set, the key is either added (if
  173.      * duplicates are permitted) or an error is returned.
  174.      *
  175.      * Pages are split as required.
  176.      */
  177.     switch (flags) {
  178.     case R_NOOVERWRITE:
  179.         if (!exact)
  180.             break;
  181.         /*
  182.          * One special case is if the cursor references the record and
  183.          * it's been flagged for deletion.  Then, we delete the record,
  184.          * leaving the cursor there -- this means that the inserted
  185.          * record will not be seen in a cursor scan.
  186.          */
  187.         if (ISSET(t, B_DELCRSR) && t->bt_bcursor.pgno == h->pgno &&
  188.             t->bt_bcursor.index == index) {
  189.             CLR(t, B_DELCRSR);
  190.             goto delete;
  191.         }
  192.         mpool_put(t->bt_mp, h, 0);
  193.         return (RET_SPECIAL);
  194.     default:
  195.         if (!exact || !ISSET(t, B_NODUPS))
  196.             break;
  197. delete:        if (__bt_dleaf(t, h, index) == RET_ERROR) {
  198.             mpool_put(t->bt_mp, h, 0);
  199.             return (RET_ERROR);
  200.         }
  201.         break;
  202.     }
  203.  
  204.     /*
  205.      * If not enough room, or the user has put a ceiling on the number of
  206.      * keys permitted in the page, split the page.  The split code will
  207.      * insert the key and data and unpin the current page.  If inserting
  208.      * into the offset array, shift the pointers up.
  209.      */
  210.     nbytes = NBLEAFDBT(key->size, data->size);
  211.     if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
  212.         if ((status = __bt_split(t, h, key,
  213.             data, dflags, nbytes, index)) != RET_SUCCESS)
  214.             return (status);
  215.         goto success;
  216.     }
  217.  
  218.     if (index < (nxtindex = NEXTINDEX(h)))
  219.         memmove(h->linp + index + 1, h->linp + index,
  220.             (nxtindex - index) * sizeof(indx_t));
  221.     h->lower += sizeof(indx_t);
  222.  
  223.     h->linp[index] = h->upper -= nbytes;
  224.     dest = (char *)h + h->upper;
  225.     WR_BLEAF(dest, key, data, dflags);
  226.  
  227.     if (t->bt_order == NOT)
  228.         if (h->nextpg == P_INVALID) {
  229.             if (index == NEXTINDEX(h) - 1) {
  230.                 t->bt_order = FORWARD;
  231.                 t->bt_last.index = index;
  232.                 t->bt_last.pgno = h->pgno;
  233.             }
  234.         } else if (h->prevpg == P_INVALID) {
  235.             if (index == 0) {
  236.                 t->bt_order = BACK;
  237.                 t->bt_last.index = 0;
  238.                 t->bt_last.pgno = h->pgno;
  239.             }
  240.         }
  241.  
  242.     mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  243.  
  244. success:
  245.     if (flags == R_SETCURSOR) {
  246.         t->bt_bcursor.pgno = e->page->pgno;
  247.         t->bt_bcursor.index = e->index;
  248.     }
  249.     SET(t, B_MODIFIED);
  250.     return (RET_SUCCESS);
  251. }
  252.  
  253. #ifdef STATISTICS
  254. u_long bt_cache_hit, bt_cache_miss;
  255. #endif
  256.  
  257. /*
  258.  * BT_FAST -- Do a quick check for sorted data.
  259.  *
  260.  * Parameters:
  261.  *    t:    tree
  262.  *    key:    key to insert
  263.  *
  264.  * Returns:
  265.  *     EPG for new record or NULL if not found.
  266.  */
  267. static EPG *
  268. bt_fast(t, key, data, exactp)
  269.     BTREE *t;
  270.     const DBT *key, *data;
  271.     int *exactp;
  272. {
  273.     PAGE *h;
  274.     size_t nbytes;
  275.     int cmp;
  276.  
  277.     if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
  278.         t->bt_order = NOT;
  279.         return (NULL);
  280.     }
  281.     t->bt_cur.page = h;
  282.     t->bt_cur.index = t->bt_last.index;
  283.  
  284.     /*
  285.      * If won't fit in this page or have too many keys in this page, have
  286.      * to search to get split stack.
  287.      */
  288.     nbytes = NBLEAFDBT(key->size, data->size);
  289.     if (h->upper - h->lower < nbytes + sizeof(indx_t))
  290.         goto miss;
  291.  
  292.     if (t->bt_order == FORWARD) {
  293.         if (t->bt_cur.page->nextpg != P_INVALID)
  294.             goto miss;
  295.         if (t->bt_cur.index != NEXTINDEX(h) - 1)
  296.             goto miss;
  297.         if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0)
  298.             goto miss;
  299.         t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index;
  300.     } else {
  301.         if (t->bt_cur.page->prevpg != P_INVALID)
  302.             goto miss;
  303.         if (t->bt_cur.index != 0)
  304.             goto miss;
  305.         if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0)
  306.             goto miss;
  307.         t->bt_last.index = 0;
  308.     }
  309.     *exactp = cmp == 0;
  310. #ifdef STATISTICS
  311.     ++bt_cache_hit;
  312. #endif
  313.     return (&t->bt_cur);
  314.  
  315. miss:
  316. #ifdef STATISTICS
  317.     ++bt_cache_miss;
  318. #endif
  319.     t->bt_order = NOT;
  320.     mpool_put(t->bt_mp, h, 0);
  321.     return (NULL);
  322. }
  323.